package com.usian.wemedia.controller.v1;

import com.usian.apis.media.WmNewsControllerApis;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/news")
public class WmNewsController implements WmNewsControllerApis {

    @Autowired
    private WmNewsService wmNewsServiceI;

    @Override
    @PostMapping("list")
    public ResponseResult findList(@RequestBody WmNewsPageReqDto dto) {

        return wmNewsServiceI.findList(dto);
    }

    @Override
    /*@PostMapping("submit")*/
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        // 根据status区分是草稿保存还是提交审核
        if (dto.getStatus().shortValue() == WmNews.Status.NORMAL.getCode()) {
            // 草稿保存
            return wmNewsServiceI.submitNews(dto, WmNews.Status.NORMAL.getCode());
        } else if (dto.getStatus().shortValue() == WmNews.Status.SUBMIT.getCode()) {
            // 提交审核
            return wmNewsServiceI.submitNews(dto, WmNews.Status.SUBMIT.getCode());
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    @Override
    @GetMapping("one/{id}")
    public ResponseResult queryNewsById(@PathVariable Integer id) {
        return wmNewsServiceI.queryNewsById(id);
    }

    @Override
    @GetMapping("del_news/{id}")
    public ResponseResult delNews(@PathVariable Integer id) {
        return wmNewsServiceI.delNews(id);
    }

    @Override
    @PostMapping("down_or_up")
    public ResponseResult downOrUpNews(@RequestBody WmNewsDto dto) {
        return wmNewsServiceI.downOrUpNews(dto);
    }

    @GetMapping("findNewsById/{id}")
    @Override
    public WmNews findNewsById(@PathVariable Integer id){
        return wmNewsServiceI.getById(id);
    }

    @PostMapping("updateNewsStatus")
    @Override
    public ResponseResult updateNewsStatus(@RequestBody WmNews wmNews){
        wmNewsServiceI.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("findRelease")
    public List<WmNews> findRelease() {
        return wmNewsServiceI.findRelease();
    }

    @PostMapping("findList")
    @Override
    public ResponseResult findList(@RequestBody NewsAuthDto newsAuthDto){

        return wmNewsServiceI.findListAndPage(newsAuthDto);
    }

    @GetMapping("findOne/{id}")
    @Override
    public ResponseResult findOne(@PathVariable Integer id){
        return wmNewsServiceI.findOne(id);
    }
}
